package com.jugheadzhou.mqtt.consumer.handler;

import com.jugheadzhou.mqtt.common.TopicEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * mqtt topic中转
 *
 * @author : jugheadzhou
 */
@Slf4j
@Component
public class MqttMessageHandler implements MessageHandler {

    @Resource
    private MqttTopicHandler topicHandler;

    /**
     * 消息会在这进行对topic的处理
     * <p>重连后</p>
     */
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        //从这里来对消息进行分topic处理
        String topic = (String) message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC);
//        boolean exist = TopicEnum.isExist(topic);
//        if (!exist) {
//            log.info("当前topic:{}未注册！不做处理！", topic);
//            return;
//        }
        String payload = new String((byte[]) message.getPayload());
        log.info("mqtt topic:{} received payload:{}", topic, payload);
        // topic消费处理
//        topicHandler.handler(topic, payload);
    }
}

